package cn.supermaster.management.web.rest.system;

import cn.supermaster.management.common.annotation.Log;
import cn.supermaster.management.common.domain.SystemDictionaryData;
import cn.supermaster.management.common.domain.SystemDictionaryType;
import cn.supermaster.management.common.entity.Ztree;
import cn.supermaster.management.common.enums.BusinessType;
import cn.supermaster.management.common.page.TableDataInfo;
import cn.supermaster.management.common.response.WebResponse;
import cn.supermaster.management.web.service.system.ISDictionaryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * All rights Reserved, Designed By www.super-bytecode.com
 *
 * @ProjectName: supermaster-management
 * @Package: cn.supermaster.management.web.rest.system
 * @ClassName: SystemDictionaryRestController
 * @Description: []
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2021/2/20 11:53
 * @Version: V1.0
 * @Copyright: 2021 www.super-bytecode.com Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@RestController
@RequestMapping("/system/dict")
public class SystemDictionaryRestController {

    @Autowired
    private ISDictionaryService dictionaryService;

    //===================================================
    // dictionary_type
    //===================================================

    @PostMapping("/list")
    @RequiresPermissions("system:dict:list")
    public TableDataInfo queryDictionaryTypeList(SystemDictionaryType dictType) {
        return dictionaryService.queryDictionaryTypeList(dictType);
    }

    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:dict:export")
    @PostMapping("/export")
    public WebResponse doExportDictionaryTypes(SystemDictionaryType dictType) {
        return dictionaryService.doExportDictionaryTypes(dictType);
    }

    /**
     * 新增保存字典类型
     */
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @RequiresPermissions("system:dict:add")
    @PostMapping("/add")
    public WebResponse addDictionaryType(@Validated SystemDictionaryType dictType) {
        return dictionaryService.addDictionaryType(dictType);
    }

    /**
     * 修改保存字典类型
     */
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:dict:edit")
    @PostMapping("/edit")
    public WebResponse modifyDictionaryType(@Validated SystemDictionaryType dictType) {
        return dictionaryService.modifyDictionaryType(dictType);
    }

    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @RequiresPermissions("system:dict:remove")
    @PostMapping("/remove")
    public WebResponse deleteDictionaryType(String ids) {
        return dictionaryService.deleteDictionaryType(ids);
    }

    /**
     * 清空缓存
     */
    @RequiresPermissions("system:dict:remove")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @GetMapping("/clearCache")
    public WebResponse cleanDictionaryCache() {
        return dictionaryService.cleanDictionaryCache();
    }

    /**
     * 校验字典类型
     */
    @PostMapping("/checkDictTypeUnique")
    public String doCheckDictionaryTypeUnique(SystemDictionaryType dictType) {
        return dictionaryService.doCheckDictionaryTypeUnique(dictType);
    }

    /**
     * 加载字典列表树
     */
    @GetMapping("/treeData")
    public List<Ztree> loadDictionaryTree() {
        return dictionaryService.loadDictionaryTree();
    }

    //===================================================
    // dictionary_data
    //===================================================
    @PostMapping("/data/list")
    @RequiresPermissions("system:dict:list")
    public TableDataInfo queryDictionaryDataList(SystemDictionaryData dictData) {

        return dictionaryService.queryDictionaryDataList(dictData);

    }

    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:dict:export")
    @PostMapping("/data/export")
    public WebResponse doExportDictionaryDatas(SystemDictionaryData dictData) {
        return dictionaryService.doExportDictionaryDatas(dictData);
    }

    /**
     * 新增保存字典类型
     */
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @RequiresPermissions("system:dict:add")
    @PostMapping("/data/add")
    public WebResponse addDictionaryData(@Validated SystemDictionaryData dictData) {
        return dictionaryService.addDictionaryData(dictData);
    }

    /**
     * 修改保存字典类型
     */
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:dict:edit")
    @PostMapping("/data/edit")
    public WebResponse modifyDictionaryData(@Validated SystemDictionaryData dictData) {
        return dictionaryService.modifyDictionaryData(dictData);
    }

    @Log(title = "字典数据", businessType = BusinessType.DELETE)
    @RequiresPermissions("system:dict:remove")
    @PostMapping("/data/remove")
    public WebResponse deleteDictionaryDatas(String ids) {
        return dictionaryService.deleteDictionaryDatas(ids);
    }
}